package lexicalUnit;

import chalmersbot.Absyn.QWordQHowToken;
import chalmersbot.Absyn.QWordQWhatToken;
import chalmersbot.Absyn.QWordQWhichToken;
import chalmersbot.Absyn.QWordQWordToken;
import chalmersbot.Absyn.QWord.Visitor;

public class CheckQWord implements Visitor<String, Environ> {

	@Override
	public String visit(QWordQWordToken p, Environ env) {
		if (p.qwordtoken_.compareTo("where")==0)
			return where(env);
		if (p.qwordtoken_.compareTo("what")==0)
			return what(env);
		if (p.qwordtoken_.compareTo("when")==0)
			return when(env);
		if (p.qwordtoken_.compareTo("who")==0)
			return who(env);
		if (p.qwordtoken_.compareTo("why")==0)
			return why(env);
		return null;
	}
	
	private String where(Environ env) {
		env.setTarget(Environ.PLACE);
		return null;
	}

	private String what(Environ env) {
		if (env.getTarget()==-1){
			if (env.getKeywordClass()!=-1)
				env.setTarget(env.getKeywordClass());
			else
				env.setTarget(Environ.THING);
		}	
		return null;
	}	
	
	private String when(Environ env) {
		env.setTarget(Environ.PERIOD);
		return null;
	}
	
	private String who(Environ env) {
		env.setTarget(Environ.PERSON);
		env.setKind_of_answer(Environ.WHO_ANSWER);
		if (env.getKeyword()==null)
			env.setKeyword(Environ.getLastKeyWord());
		return env.generateAnswer() + " " + env.getVerb() + " " + env.getSubject();
	}
	
	private String why(Environ env) {
		throw new LexicalException("I am sorry, we aren't be able to answer why questions");
	}

	@Override
	public String visit(QWordQWhichToken p, Environ env) {
		return what(env);
	}

	@Override
	public String visit(QWordQHowToken p, Environ arg) {
		throw new LexicalException("I am sorry, we aren't be able to answer how questions");
	}

	@Override
	public String visit(QWordQWhatToken p, Environ env) {
		return what(env);
	}

}
